﻿using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraSplashScreen;
using System.Collections.Generic;
using System.Windows.Forms;

namespace EyFramework.Library
{
    /// <summary>
    /// devexpress 工具类
    /// </summary>
    public class DevPublic
    {
        private static DevExpress.XtraSplashScreen.SplashScreenManager _loading;

        public static SplashScreenManager GetLoading(Form form)
        {
            if (_loading == null)
            {
                _loading = new DevExpress.XtraSplashScreen.SplashScreenManager(form, typeof(global::EyFramework.Library.LoadingForm), true, true);
                _loading.ClosingDelay = 0;
            }
            return _loading;
        }

        public static void SetGridControlView(DevExpress.XtraGrid.Views.Grid.GridView dgvData, IList<GridColumnInfo> columnAlias, bool pShowLine, bool pAlternatingRow)
        {
            GridColumnInfo gci = new GridColumnInfo();
            foreach (DevExpress.XtraGrid.Columns.GridColumn column in dgvData.Columns)
            {
                gci.COL_CDE = column.FieldName.ToUpper();
                var gIndex = columnAlias.IndexOf(gci);
                if (gIndex < 0)
                {
                    column.Visible = false;
                }
                else
                {
                    column.VisibleIndex = gIndex;
                    column.Caption = columnAlias[gIndex].COL_NM;
                    if (columnAlias[gIndex].COL_WIDTH != -1)
                    {
                        column.Width = columnAlias[gIndex].COL_WIDTH;
                    }
                    column.OptionsColumn.AllowEdit = !columnAlias[gIndex].COL_READONLY;
                    if (columnAlias[gIndex].Formattype != DevExpress.Utils.FormatType.None)
                    {
                        column.DisplayFormat.FormatType = columnAlias[gIndex].Formattype;
                        column.DisplayFormat.FormatString = columnAlias[gIndex].Formatstring;
                    }

                    if (columnAlias[gIndex].Summaryitemtype != DevExpress.Data.SummaryItemType.None)
                    {
                        column.SummaryItem.SummaryType = columnAlias[gIndex].Summaryitemtype;
                        column.SummaryItem.DisplayFormat = columnAlias[gIndex].Sumdisplayformat;
                    }

                    if (columnAlias[gIndex].Columnedit != null)
                    {
                        column.ColumnEdit = columnAlias[gIndex].Columnedit;
                    }
                }
            }
            if (pShowLine)
            {
                dgvData.OptionsView.ShowIndicator = true;
                dgvData.IndicatorWidth = 40;
                dgvData.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(dgv_data_CustomDrawRowIndicator);
            }

            if (pAlternatingRow)
            {
                dgvData.Appearance.EvenRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(248)))), ((int)(((byte)(255)))));
                dgvData.Appearance.EvenRow.Options.UseBackColor = true;
                dgvData.OptionsView.EnableAppearanceEvenRow = true;
            }
        }

        private static void dgv_data_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle > -1)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        public static void SetBindGridControlView(GridView dgvData, bool pShowLine, bool pAlternatingRow)
        {
            if (pShowLine)
            {
                dgvData.OptionsView.ShowIndicator = true;
                dgvData.IndicatorWidth = 40;
                dgvData.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(dgv_data_CustomDrawRowIndicator);
            }

            if (pAlternatingRow)
            {
                dgvData.Appearance.EvenRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(248)))), ((int)(((byte)(255)))));
                dgvData.Appearance.EvenRow.Options.UseBackColor = true;
                dgvData.OptionsView.EnableAppearanceEvenRow = true;
            }
        }
    }
}